package com.lill.stock.service.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lill.common.component.model.P;
import com.lill.common.component.util.IdUtil;
import com.lill.org.model.CompanyBranchModel;
import com.lill.org.service.ICompanyBranchService;
import com.lill.product.model.ProductBaseModel;
import com.lill.product.model.ProductCarModel;
import com.lill.product.model.StorageInfoModel;
import com.lill.product.service.IProductBaseService;
import com.lill.product.service.IProductCarService;
import com.lill.product.service.IStorageInfoService;
import com.lill.stock.dao.IStockRealDao;
import com.lill.stock.model.StockRealModel;
import com.lill.stock.service.IStockRealService;
import com.lill.system.service.ISysImportService;

/**
 * 实时库存Service实现类
 *
 * @author lill
 * @date 2021-09-25 11:41:47
 */
@Service("com.lill.stock.service.impl.StockRealServiceImpl")
public class StockRealServiceImpl implements IStockRealService {

    @Autowired
    private IStockRealDao stockRealDao;
    @Autowired
    private IProductBaseService productBaseService;
    @Autowired
    private IProductCarService productCarService;
    @Autowired
    private ICompanyBranchService companyBranchService;
    @Autowired
    private IStorageInfoService storageInfoService;
    @Autowired
    private ISysImportService sysImportService;

    public StockRealModel queryObject(String id) {
        return stockRealDao.queryObject(id);
    }

    public List<StockRealModel> queryList(Map<String, Object> map) {
        List<StockRealModel> list = stockRealDao.queryList(map);
        Map<String, String> mapInfo=new HashMap<String, String>();
        Map<String, Object> mapCar=new HashMap<String, Object>();
        if(CollectionUtils.isNotEmpty(list)){
            for (StockRealModel stockRealModel : list) {
                if(mapInfo.containsKey(stockRealModel.getProductId())){
                    stockRealModel.setProductName(mapInfo.get(stockRealModel.getProductId()));
                }else{
                    ProductBaseModel productBase=productBaseService.queryObject(stockRealModel.getProductId());
                    if(null!=productBase){
                        stockRealModel.setProductName(productBase.getAllName());
                        mapInfo.put(stockRealModel.getProductId(), productBase.getAllName());
                    }else{
                        mapInfo.put(stockRealModel.getProductId(), "");
                    }
                }
                if(mapInfo.containsKey(stockRealModel.getBranchId())){
                    stockRealModel.setBranchName(mapInfo.get(stockRealModel.getBranchId()));
                }else{
                    CompanyBranchModel companyBranch=companyBranchService.queryObject(stockRealModel.getBranchId());
                    if(null!=companyBranch){
                        stockRealModel.setBranchName(companyBranch.getName());
                        mapInfo.put(stockRealModel.getBranchId(), companyBranch.getName());
                    }else{
                        mapInfo.put(stockRealModel.getBranchId(), "");
                    }
                }
                if(mapInfo.containsKey(stockRealModel.getStorageId())){
                    stockRealModel.setStorageName(mapInfo.get(stockRealModel.getStorageId()));
                }else{
                    StorageInfoModel storageInfo=storageInfoService.queryObject(stockRealModel.getStorageId());
                    if(null!=storageInfo){
                        stockRealModel.setStorageName(storageInfo.getName());
                        mapInfo.put(stockRealModel.getStorageId(), storageInfo.getName());
                    }else{
                        mapInfo.put(stockRealModel.getStorageId(), "");
                    }
                }
                
                if(mapCar.containsKey(stockRealModel.getProductId())){
                	stockRealModel.setProductVo(mapCar.get(stockRealModel.getProductId()));
                }else{
                	ProductCarModel carModel=productCarService.queryObject(stockRealModel.getProductId());
                	if(null!=carModel){
                		stockRealModel.setProductVo(carModel);
                		mapCar.put(stockRealModel.getProductId(), carModel);
                	}
                }
            }
        }
        return list;
    }

    public int queryTotal(Map<String, Object> map) {
        return stockRealDao.queryTotal(map);
    }

    public int save(StockRealModel stockReal) {
        stockReal.setId(IdUtil.createIdbyUUID());
        return stockRealDao.save(stockReal);
    }

    public int update(StockRealModel stockReal) {
        return stockRealDao.update(stockReal);
    }
    
	public int deleteLogicByMap(Map<String, Object> map) {
		return stockRealDao.deleteLogicByMap(map);
	}

    public int saveBatch(List<StockRealModel> list) {
        return stockRealDao.saveBatch(list);
    }

	public int updateStore(String vin,String personId) {
		Map<String, Object> vinMap=new HashMap<String, Object>();
		vinMap.put("vin", vin);
		vinMap.put("personId", personId);
		return stockRealDao.updateStore(vinMap);
	}

	public int saleRealStock(String[] ids, String userId) {
		int result=0;
		if(null!=ids&&ids.length>0){
			for (String id : ids) {
				result+=stockRealDao.updateSaleRealStock(id);
			}
		}
		return result;
	}

	public P importData(Map<String, Object> baseMap, String booktable, InputStream in, boolean xlsxFormat) {
		P result=P.ok();
		int rowNumStart=0;
		List<Map<String, Object>> importDataList = sysImportService.readDataListFromExcel(rowNumStart,booktable, in, xlsxFormat);
		if(CollectionUtils.isNotEmpty(importDataList)){
			List<Map<String, Object>> importDataUseList=new ArrayList<Map<String, Object>>();
			for (Map<String, Object> mapImport : importDataList) {
				Map<String, Object> useMap=new HashMap<String, Object>();
				if(null!=mapImport.get("code")&&StringUtils.isNotEmpty(mapImport.get("code").toString().trim())){
					useMap.putAll(mapImport);
					useMap.putAll(baseMap);
					useMap.put("status",1);
					useMap.put("route_date",new Date());
					useMap.put("top_classify_id",ProductCarModel.classify);
					useMap.put("id",IdUtil.createIdbyUUID());
					importDataUseList.add(useMap);
				}
			}
			if(!importDataUseList.isEmpty()){
				stockRealDao.saveBatchReal(importDataUseList);
			}
		}
		return result;
	}

	public Map<String, Object> queryStatStock(Map<String, Object> params) {
		return stockRealDao.queryStatStock(params);
	}

	public int updateCancelOrder(String id) {
		return stockRealDao.updateCancelOrder(id);
	}
	
	public int updateRealStock(Map<String, Object> realMap) {
		return stockRealDao.updateRealStock(realMap);
	}
}
